﻿using System;
using Glue;
using Xunit;

namespace GlueExamples
{
    namespace AutomaticallyGenerateRelations
    {

        public class Autorelating
        {
            private class GuiPerson
            {
                public String FirstName { get; set; }
                public String LastName { get; set; }
            }

            private class DomainPerson
            {
                public String FirstName { get; set; }
                public String LastName { get; set; }
            }

            [Fact]
            public void Should_map_properties_with_equal_names()
            {
                var domainPerson = new DomainPerson {FirstName = "Tore", LastName = "TheCoder"};
                var guiPerson = new GuiPerson();

                var mapping = new Mapping<DomainPerson, GuiPerson>();
                mapping.AutoRelateEqualNames();
                
                mapping.Map(domainPerson, guiPerson);
                
                Assert.Equal("Tore", guiPerson.FirstName);
                Assert.Equal("TheCoder", guiPerson.LastName);
            }
        }

        public class When_Autorelation_Unidirectional
        {
            private readonly Mapping<DomainPerson, GuiPerson> mapping;
            private readonly DomainPerson domainPerson;
            private readonly GuiPerson guiPerson;

            private class GuiPerson
            {
                public String FirstName { get; set; }
                public String LastName { get; set; }
            }

            private class DomainPerson
            {
                public String FirstName { get; set; }
                public String LastName { get; set; }
            }

            public When_Autorelation_Unidirectional()
            {
                domainPerson = new DomainPerson();
                guiPerson = new GuiPerson();

                mapping = new Mapping<DomainPerson, GuiPerson>();
                mapping.AutoRelateEqualNames();
                mapping.RelateTowardsRight(domainperson => domainperson.LastName, guiperson => guiperson.LastName);
            }

            [Fact]
            public void Should_map_towardsRight()
            {
                domainPerson.FirstName = "Tore";
                domainPerson.LastName = "TheCoder";

                mapping.Map(domainPerson, guiPerson);

                Assert.Equal("Tore", guiPerson.FirstName);
                Assert.Equal("TheCoder", guiPerson.LastName);
            }

            [Fact]
            public void Should_not_map_towardsLeft()
            {
                guiPerson.FirstName = "Tore";
                guiPerson.LastName = "TheCoder";
                domainPerson.FirstName = "-";
                domainPerson.LastName = "-";

                mapping.Map(guiPerson, domainPerson);

                Assert.Equal("Tore", domainPerson.FirstName);
                Assert.Equal("-", domainPerson.LastName);
            }
        }
    }
}
